#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "job_schd.h"
#include "ipc_def.h"

unsigned int jid;
int cli_sockfd;

void print_usage(int exit_status)
{
    perror("Usage: deq jid\n");
    perror(USAGE_IDENT "jid: job id.");
    perror(USAGE_IDENT "The effective user id should match to comlete the operation successfully. Otherwise, error would occur.\n");
    exit(exit_status);
}

void init_args(int argc, char *argv[])
{
    if (argc < 2) {
        perror("too few argument\n");
        print_usage(EXIT_FAILURE);
    }
    if ((jid = atoi(argv[1])) < 0) {
        perror("an invalid job id was given.\n");
    }
}

void send_deq_info(void)
{
    char byte;
    uid_t euid;

    byte = OP_DEQ;
    send_item(cli_sockfd, TAG_OPCODE, sizeof(unsigned char), &byte);

    euid = geteuid();
    send_item(cli_sockfd, TAG_UID, sizeof(uid_t), (char *)&euid);

    send_item(cli_sockfd, TAG_JID, sizeof(unsigned int), (char *)&jid);
    send_item(cli_sockfd, TAG_END, 0, NULL);
}

int main(int argc, char *argv[])
{
    init_args(argc, argv);
    if ((cli_sockfd = init_conn()) < 0) {
        exit(EXIT_FAILURE);
    }
    send_deq_info();
    close(cli_sockfd);
    return 0;
}

